﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Http;

using ERPStore.Web.Extensions;

using CloudSoft.Extensions;

namespace ERPStore.Coupons.Controllers
{
	public class IncentiveApiController : ApiController
	{
		public IncentiveApiController(Services.AdminIncentiveService adminIncentiveService
			, ERPStore.Services.ICartService cartService
			, ERPStore.Services.IIncentiveService incentiveService)
		{
			this.AdminIncentiveService = adminIncentiveService;
			this.CartService = cartService;
			this.IncentiveService = incentiveService;
		}

		protected Services.AdminIncentiveService AdminIncentiveService { get; private set; }
		protected ERPStore.Services.ICartService CartService { get; private set; }
		protected ERPStore.Services.IIncentiveService IncentiveService { get; private set; }

		protected virtual new ERPStore.Models.UserPrincipal User
		{
			get
			{
				return base.User as ERPStore.Models.UserPrincipal;
			}
		}

		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public DateTime Ping()
		{
			return DateTime.Now;
		}

		[System.Web.Http.HttpPost]
		[System.Web.Http.HttpPut]
		public IEnumerable<ERPStore.Models.BrokenRule> PutCouponToCart(string id, [FromUri] string cartCode)
		{
			var cart = CartService.GetCartByCode(cartCode);
			if (cart == null)
			{
				return new List<ERPStore.Models.BrokenRule>();
			}
			var brokenRules = IncentiveService.ValidateUse(cart, id, User);
			if (brokenRules.IsNullOrEmpty())
			{
				IncentiveService.AddCoupon(cart, id, User);
			}
			return brokenRules;
		}

		public void DeleteCoupon(string id)
		{
			var cart = CartService.GetCurrentCart(User);
			if (cart == null)
			{
				return;
			}

			IncentiveService.RemoveCoupon(cart, id, User);
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public Models.Coupon CreateCoupon()
		{
			var coupon = AdminIncentiveService.CreateCoupon();

			return coupon;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public bool CodeExists(string id)
		{
			return AdminIncentiveService.IsCodeExists(id);
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpPost]
		public IEnumerable<ERPStore.Models.BrokenRule> ValidateCoupon(Models.Coupon model)
		{
			return AdminIncentiveService.ValidateCoupon(model);
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		public IEnumerable<Models.CouponProviderInfo> GetProviderList()
		{
			var list = AdminIncentiveService.GetCouponProviderList();
			var result = new List<Models.CouponProviderInfo>();
			foreach (var item in list)
			{
				var info = new Models.CouponProviderInfo();
				info.Name = item.Name;
				info.Description = item.Description;
				result.Add(info);
			}
			return result;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public Models.CouponListResult GetCouponList(Models.CouponListFilter filter)
		{
			int count = 0;
			var list = AdminIncentiveService.GetCouponList(filter, out count);

			var result = new Models.CouponListResult();
			result.TotalCount = count;
			result.List.AddRange(list);
			return result;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpPut]
		[System.Web.Http.HttpPost]
		public Models.Coupon SaveCoupon(Models.Coupon coupon)
		{
			AdminIncentiveService.SaveCoupon(coupon);
			return coupon;
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		[System.Web.Http.HttpPost]
		public Models.Coupon GetCouponById(int Id)
		{
			return AdminIncentiveService.GetCouponById(Id);
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpDelete]
		[System.Web.Http.HttpPost]
		public void DeleteCoupon(int couponId)
		{
			throw new NotImplementedException();
		}

		[ERPStore.Web.Controllers.ActionFilters.ApiAuthorizedOperation]
		[System.Web.Http.HttpGet]
		public string GenerateCode()
		{
			return AdminIncentiveService.GenerateCode();
		}

	}
}
